namespace ETestIT.UI.Presentation
{
    using System;
    using System.Web;
    using System.Web.Security;
    using Domain.Repositories;
    using Domain.Services;

    public class AuthenticationServiceImpl : IAuthenticationService
    {
        private readonly IUserRepository userRepository;

        public AuthenticationServiceImpl(IUserRepository userRepository)
        {
            this.userRepository = userRepository;
        }

        #region IAuthenticationService Members

        public void SetAuthCookie(string cookieName)
        {
            FormsAuthentication.SetAuthCookie(cookieName, false);
        }

        public void SignOut()
        {
            FormsAuthentication.SignOut();
            HttpContext.Current.Response.Redirect("/Login.aspx");
        }

        public bool IsValide(string userName, string pass)
        {
            if (userRepository.GetByNameAndPassword(userName, pass) != null)
            {
                SetAuthCookie(userName);
                return true;
            }

            return false;
        }

        #endregion
    }
}